'use strict';

/**
 * @ngdoc function
 * @name webApp.controller:CommunityCtrl
 * @description
 * # CommunityCtrl
 * Controller of the webApp
 */

angular.module('webApp')
  .controller('community', function ($scope,$rootScope,arrayBox,promiseCommunity,$animate,jqLite,cookie) {
    $scope.$on('$stateChangeSuccess',function(){
      //监听路由再格式化
      $animate['removeClass'](jqLite.$('moreBut'),'deg45');
      $scope.tagTitle = '推荐标签';
      promiseCommunity.getTags('hot').then(function(data){
        $scope.floatDatas = data;
        $scope.communicateData = [];
        $scope.tagSelet = false;
        tagBox = [];
      });
    });
    //社区讨论
    $scope.tagTitle = '推荐标签';
    //动画
    $scope.communicateShow = true;
    $scope.slideShow = false;
    //更多
    $scope.more = function(){
      //动画
      if(jqLite.$('moreBut').hasClass('deg45')){
        $animate['removeClass'](jqLite.$('moreBut'),'deg45');
      }
      else{
        $animate['addClass'](jqLite.$('moreBut'),'deg45');
      }
      $scope.slideShow = !$scope.slideShow;
      $scope.communicateShow = !$scope.communicateShow;
      if($scope.communicateShow){
        $scope.tagTitle = '热门标签';
        promiseCommunity.getTags('hot').then(function(data){
          var floatDatas = data;
          var len = $scope.floatDatas.length;
          for(var i = 0;i < floatDatas.length;i++){
            floatDatas[i].selet = false;
          }
          for(var i = 0;i < floatDatas.length;i++){
            for(var k = 0;k < len;k++){
              if($scope.floatDatas[k].id == floatDatas[i].id){
                if($scope.floatDatas[k].selet){
                  floatDatas[i].selet = true;
                }
              }
            }
          }
          $scope.floatDatas = floatDatas;
        });
      }
      else{
        $scope.tagTitle = '推荐标签';
        promiseCommunity.getTags('all').then(function(data){
          var floatDatas = data;
          for(var i = 0;i < floatDatas.length;i++){
            floatDatas[i].selet = false;
          }
          for(var i = 0;i < floatDatas.length;i++){
            for(var k = 0;k < tagBox.length;k++){
              if(floatDatas[i].id == tagBox[k]){
                floatDatas[i].selet = true;
              }
            }
          }
          $scope.floatDatas = floatDatas;
        });
      }
    };
    promiseCommunity.getTags('hot').then(function(){
      //默认
      promiseCommunity.getTags('hot').then(function(data){
        var floatDatas = data;
        for(var i = 0;i < floatDatas.length;i++){
          floatDatas[i].selet = false;
        }
        $scope.floatDatas = floatDatas;
      });
    });

    var allData;
    var tagBox = [];
    promiseCommunity.getTags('all').then(function(data){
      allData = data;
    });
    //选择标签
    $scope.tag = function(id){
      var floatDatas = allData;
      if(allData){
        tagBox.push(id);
        tagBox = arrayBox.shortToBig(arrayBox.delSame(tagBox));
        var tagSelet = '';
        for(var i = 0;i < floatDatas.length;i++){
          for(var k = 0;k < tagBox.length;k++){
            if(floatDatas[i].id == tagBox[k]){
              if(tagSelet != ''){
                tagSelet = tagSelet + '+' + floatDatas[i].name;
              }
              else{
                tagSelet = floatDatas[i].name;
              }
            }
          }
        }
        $scope.tagSelet = tagSelet;
        var len = $scope.floatDatas.length;
        for(var i = 0;i < len;i++){
          if($scope.floatDatas[i].id == id){
            $scope.floatDatas[i].selet = !$scope.floatDatas[i].selet;
          }
        }
        if(tagBox != ''){
          promiseCommunity.getCommunityComment(tagBox).then(function(data){
            for(var i = 0;i < data.length;i++){
              //修正外部内容
              if(data[i].owner.name == ''){
                data[i].owner.name = data[i].owner.UserEmail;
              }
              data[i].user = data[i].owner;
              //修正来源
              data[i].article.origin = data[i].article.Source;
              //修正回复内容
              for(var k = 0;k < data[i].replies.length;k++){
                if(data[i].replies[k].user.name == ''){
                  data[i].replies[k].user.name = data[i].replies[k].user.UserEmail;
                }
              }
            }
            $scope.communicateData = data;
          });
        }
      }
    };
    //监听评论消息
    $rootScope.$on('/root/community',function(ele,data){
      if(data.action == '写回复'){
        for(var i = 0;i < $scope.communicateData.length;i++){
          if($scope.communicateData[i].id == cookie.getCookie('replayId')){
            if($scope.communicateData[i].replies){
              $scope.communicateData[i].replies.splice(0,0,data.data);
            }
            else{
              $scope.communicateData[i].replies.replies = [data.data];
            }
          }
        }
      }
    });
});
